#include "thumbnail.h"
#define FPS 1.0/60.0

Thumbnail::Thumbnail(item& _data, QWidget *parent) :
    QWidget(parent), rectOff(0,75)
{
    setContextMenuPolicy(Qt::CustomContextMenu);
    data = _data;
    setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    ani_timer = new QTimer();
    ani_timer->setSingleShot(false);
    connect(ani_timer, SIGNAL(timeout()), this, SLOT(update()));

    ani = new QPropertyAnimation(this, "rectOff");
    ani->setDuration(500);
    ani->setStartValue(QPoint(0,75));
    ani->setEndValue(QPoint(0,0));

    marq_ani = new QPropertyAnimation(this, "tagMarq");
    marq_ani->setDuration(7000);
    marq_ani->setStartValue(QPoint(0,0));
    marq_ani->setKeyValueAt(0.10,QPoint(0,0));
    marq_ani->setKeyValueAt(0.20,QPoint(-150,0));
    marq_ani->setKeyValueAt(0.40,QPoint(-150,0));
    marq_ani->setKeyValueAt(0.50,QPoint(-300,0));
    marq_ani->setKeyValueAt(0.60,QPoint(-300,0));
    marq_ani->setKeyValueAt(0.80,QPoint(-450,0));
    marq_ani->setKeyValueAt(0.800001,QPoint(450,0));
    marq_ani->setEndValue(QPoint(0,0));
    marq_ani->setLoopCount(-1);

    connect(ani,SIGNAL(finished()),marq_ani,SLOT(start()));
    selected = false;
    treeRef = 0;

    menu = new QMenu();
    open = new QAction(tr("open"),this);
    edit = new QAction(tr("edit"),this);
    del = new QAction(tr("delete"),this);

    menu->addAction(open);
    menu->addAction(edit);
    menu->addAction(del);
}

QSize Thumbnail::sizeHint() const
{
    return QSize(150,150);

}

void Thumbnail::renderImage()
{
    if(pixmap.isNull())return;
    QPainter paint(this);


    paint.setRenderHints(QPainter::Antialiasing |
                         QPainter::SmoothPixmapTransform |
                         QPainter::HighQualityAntialiasing |
                         QPainter::TextAntialiasing);
    QPainterPath rr;
    rr.addRoundedRect(0,0,150,150,7,7);
    paint.setClipping(true);
    paint.setClipPath(rr);

    paint.setBrush(QBrush(QColor(255,255,255,220)));
    paint.setPen(Qt::NoPen);

    QFont font = QFont("Georgia",10,QFont::Bold);
    paint.setFont(font);

    paint.drawPixmap(QRect(0,0,this->width(),this->height()),pixmap);


    paint.setBrush(QBrush(QColor(150,150,150,200)));
    if(selected)
    paint.drawRoundedRect(0,0,150,150,7,7);

    paint.setBrush(QBrush(QColor(0,0,0,200)));
    paint.drawRect(QRect(0 + rectOff.x(),75 + rectOff.y(),150,75));

    paint.setPen(QPen(QColor(220,220,220)));

    paint.drawText(QRectF(5 + rectOff.x() + tagMarq.x(),80 + rectOff.y() + tagMarq.y(),140,70),Qt::AlignHCenter | Qt::TextWordWrap,data.name);
    paint.drawText(QRectF(155 + rectOff.x() + tagMarq.x(),80 + rectOff.y() + tagMarq.y(),140,70),Qt::AlignHCenter | Qt::TextWordWrap,data.tags.join(", "));
    paint.drawText(QRectF(305 + rectOff.x() + tagMarq.x(),80 + rectOff.y() + tagMarq.y(),140,70),Qt::AlignHCenter | Qt::TextWrapAnywhere,data.address);

    paint.setPen(QPen(QColor(220,220,220)));
}

void Thumbnail::setImage(const QString &fileName)
{
    image.load(fileName);
    pixmap = QPixmap::fromImage(image);
    renderImage();
}

void Thumbnail::setData(const item &_data, QTreeWidgetItem *ref)
{
    data = _data;
    treeRef = ref;
    image.load(data.thumbPath);
    pixmap = QPixmap::fromImage(image);
    renderImage();
}

void Thumbnail::paintEvent(QPaintEvent *event)
{
    event->accept();
    renderImage();
}

void Thumbnail::enterEvent(QEvent *e)
{
    ani_timer->start(FPS);
    ani->start();
}

void Thumbnail::leaveEvent(QEvent *e)
{
    ani_timer->stop();
    ani->stop();
    marq_ani->stop();
    resetRectOff();
    resetTagMarq();
    update();
}

void Thumbnail::mouseDoubleClickEvent(QMouseEvent *e)
{
    QProcess* p = new QProcess();
    p->start(settingsDialog::getInstance()->getDefaultBrowserPath(),QStringList() << data.address);
    //should destroy.
}
